<?php
namespace app\admin\controller;
use think\Controller;
use think\request;
use think\Config;
use think\Db;
class Slide extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    public function listSlide(Request $request)
    {
    	$page = $request->get('page');
        $limit = $request->get('limit');
        $start = ($page - 1) * $limit;
        $condition = $request->get('condition');
        if($condition){
            $where = " `slide_name` LIKE '%$condition%'";
            $data=Db::name('slide')->where($where)->limit($start,$limit)->select();
            $count=Db::name('slide')->where($where)->count();
        } else {
            $data=Db::name('slide')->limit($start,$limit)->select();
            $count=Db::name('slide')->count();
        }
        return getTableJson(0,'success',$count,$data);
        
    }

    public function addSlide(Request $request)
    {
    	if($request->isPost()){
                $data=$request->post();
                unset($data['file']);
                $res= Db::name('slide')->insert($data);
                if($res){
                    return getJson(0,'新增成功');
                }
                return getJson(1,'新增失败');
          }
        return $this->fetch();
    }

    public function editSlide(Request $request)
    {

        $id=input('id');
 		$data=Db::name('slide')->where('id',$id)->find();
 		$this->assign('data',$data);
    	if($request->isPost()){
            $data=$request->post();
            unset($data['file']);
              $res=Db::name('slide')->where('id',$id)->update($data);
              if($res){
                return getJson(0,'修改成功');
              }
                return getJson(1,'数据没变更，无需提交');
        }
       return $this->fetch();
    }

    public function delSlide()
    {

    	$id=input('id');
        $res=Db::name('slide')->where('id',$id)->delete();
        if($res){
            return getJson(0,'删除成功');
        }
        return getJson(1,'删除失败');
        
    }

    public function batchDel(Request $request)
    {
        $ids = $request->post('ids/a');
        $table = Config::get("database.prefix") . 'slide';
        $res = Db::table($table)
                ->delete($ids);

        if($res){
            return getJson(0,'删除成功');
        }
        return getJson(1,'删除失败');
    }








   
}